<?php
/*
 * This file is part of EC-CUBE
 *
 * Copyright(c) 2000-2012 LOCKON CO.,LTD. All Rights Reserved.
 *
 * http://www.lockon.co.jp/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

// {{{ requires
require_once CLASS_EX_REALDIR . 'page_extends/LC_Page_Ex.php';

/**
 * 会員登録のページクラス.
 *
 * @package Page
 * @author LOCKON CO.,LTD.
 * @version $Id: LC_Page_Regist.php 21950 2012-07-02 11:42:51Z pineray $
 */
class LC_Show_PayMent_Details extends LC_Page_Ex {

    // {{{ properties

    // }}}
    // {{{ functions
	
    /**
     * Page を初期化する.
     *
     * @return void
     */
    function init() {
    	
        parent::init();
        $this->linkCss[] = 'css/sapaImport.css';
		//引入本页面javascript
		$this->link_javascript[] = "js/sapa/jquery.dataTables.js";
        
    }

    /**
     * Page のプロセス.
     *
     * @return void
     */
    function process() {
        parent::process();
        $this->action();
        $this->sendResponse();
       
    }

    /**
     * Page のAction.
     *
     * @return void
     */
    function action() {
    	
        switch ($this->getMode()) {
            case 'dataExport': 
            	$this->adminAuthorityCheck();
            	$this->tpl_mainpage = 'pay/show_payment_details.tpl';
            	$this->getAllPaymentInfoArr();
            	$this->dataExport();
                break;
           
            default:
            	$this->adminAuthorityCheck();
            	$this->tpl_mainpage = 'pay/show_payment_details.tpl';
            	$this->getAllPaymentInfoArr();
                break;
        }
    }

    /**
     * デストラクタ.
     *
     * @return void
     */
    function destroy() {
        parent::destroy();
    }
	
    function getAllPaymentInfoArr(){
    	$objQuery = SC_Query::getSingletonInstance();
		$this->allPaymentInfoArr = $objQuery->select('b.lastname,b.firstname,a.*','tab_pay_detail a,tab_person_users b',' a.user_id = b.id');
    	
		foreach ($this->allPaymentInfoArr as $key => $value) {
			if($value['goods_type'] == "0"){
				$this->allPaymentInfoArr[$key]['goods_type'] = "Menbers Fee";
			}elseif (strstr($value['goods_type'], 'CF')){
				$confrenceId = str_replace("CF","",$value['goods_type']);
				$confrenceId = str_replace("*","",$confrenceId);
				$confrenceId = trim($confrenceId);
				$rs = $objQuery->select("conference_title","mst_conference","id = ?", array($confrenceId));
				
				$this->allPaymentInfoArr[$key]['goods_type'] = $rs[0]["conference_title"];
			}else{
				$this->allPaymentInfoArr[$key]['goods_type'] = "Others";
			}
		}
    }
    
    function dataExport(){
	
		$csv = "";
		if(!empty($this->allPaymentInfoArr)){
			foreach ($this->allPaymentInfoArr as $key => $value) {
				$csv .=$value["id"]. ",";
				$csv .=$value["goods_type"]. ",";
				$csv .=$value["firstname"]." ".$value["lastname"].",";
				$csv .=$value["goods_name"]. ",";
				$csv .=$value["price"]. ",";
				$csv .=$value["quantity"]. ",";
				$csv .=$value["amount"]. ",";
				$csv .=$value["update_time"]. "\n";
				
			}
			header("Content-Type: application/octet-stream");
			header("Content-Disposition: attachment; filename=payMentDetail.csv");
			//header("Content-Type: text/csv; charset=UTF-8 ");
			//echo mb_convert_encoding("テスト\n","SJIS", "UTF-8");
		}
		echo($csv);
		exit();
	}
    
}
